<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户id" prop="businessId">
        <el-input
          v-model="queryParams.businessId"
          placeholder="请输入商户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户名" prop="businessName">
        <el-input
          v-model="queryParams.businessName"
          placeholder="请输入商户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['account:balance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['account:balance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['account:balance:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['account:balance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="balanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键 自增" align="center" prop="id" />
      <el-table-column label="商户ID" align="center" prop="businessId" />
      <el-table-column label="商户名称" align="center" prop="businessName" />
      <el-table-column label="流水操作类型" align="center" prop="balanceType" />
      <el-table-column label="操作类型" align="center" prop="operationType" />
      <el-table-column label="业务订单ID" align="center" prop="ywOrderId" />
      <el-table-column label="业务订单号" align="center" prop="ywOrderNo" />
      <el-table-column label="支付订单" align="center" prop="payOrderId" />
      <el-table-column label="支付订单号" align="center" prop="payOrderNo" />
      <el-table-column label="操作明细" align="center" prop="opItem" />
      <el-table-column label="操作时间" align="center" prop="opTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.opTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作金额" align="center" prop="amount" />
      <el-table-column label="新的余额" align="center" prop="balance" />
      <el-table-column label="合同ID，" align="center" prop="contractId" />
      <el-table-column label="合同编号" align="center" prop="contractNo" />
      <el-table-column label="合同类型" align="center" prop="contractType" />
      <el-table-column label="状态编码" align="center" prop="statusCode" />
      <el-table-column label="状态" align="center" prop="statusName" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="删除标识" align="center" prop="delFlg" />
      <el-table-column label="创建用户ID" align="center" prop="createUserid" />
      <el-table-column label="更新用户ID" align="center" prop="updateUserid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['account:balance:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['account:balance:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业账户流水明细(企业账户变动历史记录)对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商户ID" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入商户ID" />
        </el-form-item>
        <el-form-item label="商户名称" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="操作业务订单ID，业务订单ID" prop="ywOrderId">
          <el-input v-model="form.ywOrderId" placeholder="业务订单ID" />
        </el-form-item>
        <el-form-item label="业务订单号(收付款计划号，提现申请单号，充值单号)" prop="ywOrderNo">
          <el-input v-model="form.ywOrderNo" placeholder="收付款计划号" />
        </el-form-item>
        <el-form-item label="支付订单ID, 对应的支付订单" prop="payOrderId">
          <el-input v-model="form.payOrderId" placeholder="对应的支付订单" />
        </el-form-item>
        <el-form-item label="支付订单号" prop="payOrderNo">
          <el-input v-model="form.payOrderNo" placeholder="请输入支付订单号" />
        </el-form-item>
        <el-form-item label="操作明细" prop="opItem">
          <el-input v-model="form.opItem" placeholder="请输入操作明细" />
        </el-form-item>
        <el-form-item label="操作时间" prop="opTime">
          <el-date-picker clearable
                          v-model="form.opTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择操作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作金额
,负数时为减" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入操作金额
,负数时为减" />
        </el-form-item>
        <el-form-item label="新的余额" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入新的余额" />
        </el-form-item>
        <el-form-item label="合同ID，有对应的合同的时候记录" prop="contractId">
          <el-input v-model="form.contractId" placeholder="请输入合同ID，有对应的合同的时候记录" />
        </el-form-item>
        <el-form-item label="合同编号" prop="contractNo">
          <el-input v-model="form.contractNo" placeholder="请输入合同编号" />
        </el-form-item>
        <el-form-item label="状态编码" prop="statusCode">
          <el-input v-model="form.statusCode" placeholder="请输入状态编码" />
        </el-form-item>
        <el-form-item label="状态" prop="statusName">
          <el-input v-model="form.statusName" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="删除标识,0:未删除，1：删除" prop="delFlg">
          <el-input v-model="form.delFlg" placeholder="请输入删除标识,0:未删除，1：删除" />
        </el-form-item>
        <el-form-item label="创建用户ID" prop="createUserid">
          <el-input v-model="form.createUserid" placeholder="请输入创建用户ID" />
        </el-form-item>
        <el-form-item label="更新用户ID" prop="updateUserid">
          <el-input v-model="form.updateUserid" placeholder="请输入更新用户ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBalance, getBalance, delBalance, addBalance, updateBalance } from "@/api/account/balance";

export default {
  name: "Balance",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业账户流水明细(企业账户变动历史记录)表格数据
      balanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessId: null,
        businessName: null,
        balanceType: null,
        operationType: null,
        ywOrderId: null,
        ywOrderNo: null,
        payOrderId: null,
        payOrderNo: null,
        opItem: null,
        opTime: null,
        amount: null,
        balance: null,
        contractId: null,
        contractNo: null,
        contractType: null,
        statusCode: null,
        statusName: null,
        remarks: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createUserid: [
          { required: true, message: "创建用户ID不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateUserid: [
          { required: true, message: "更新用户ID不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询企业账户流水明细(企业账户变动历史记录)列表 */
    getList() {
      this.loading = true;
      listBalance(this.queryParams).then(response => {
        this.balanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessId: null,
        businessName: null,
        balanceType: null,
        operationType: null,
        ywOrderId: null,
        ywOrderNo: null,
        payOrderId: null,
        payOrderNo: null,
        opItem: null,
        opTime: null,
        amount: null,
        balance: null,
        contractId: null,
        contractNo: null,
        contractType: null,
        statusCode: null,
        statusName: null,
        remarks: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业账户流水明细(企业账户变动历史记录)";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBalance(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业账户流水明细(企业账户变动历史记录)";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBalance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBalance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除企业账户流水明细(企业账户变动历史记录)编号为"' + ids + '"的数据项？').then(function() {
        return delBalance(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('account/balance/export', {
        ...this.queryParams
      }, `balance_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
